//
//  SettingViewController.m
//  Thinkape-iOS
//
//  Created by tixa on 15/6/18.
//  Copyright (c) 2015年 TIXA. All rights reserved.
//

#import "SettingViewController.h"
#import "AppDelegate.h"
#import "LogineViewController.h"
#import "AboutViewController.h"
#import "ChangePasswordViewController.h"
#import "FanKuiSViewController.h"
#import "MyInforViewController.h"
@interface SettingViewController ()
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic,strong)UIAlertController *alertController;


@end


@implementation SettingViewController

@synthesize version;


- (void)viewDidLoad {
    [super viewDidLoad];
    [self.dataArray addObject:@"关于思凯普"];
    [self.dataArray addObject:@"清除缓存"];
    [self.dataArray addObject:@"修改密码"];
    [self.dataArray addObject:@"建议反馈"];
    [self.dataArray addObject:@"检查更新"];
    [self.dataArray addObject:@"个人中心"];
   
    self.tableView.tableFooterView = [[UIView alloc] init];
    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - BtnAction

- (IBAction)logout:(UIButton *)sender {
   BOOL logout = [[DataManager shareManager] removeAccount];
  [[DataManager shareManager] cleanLocalCache];
    if (logout) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"logout" object:nil];
        [self.navigationController popViewControllerAnimated:YES];
    }
}

#pragma mark - UITableView Delegate && DataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *cellid = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
    
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(13, 10, SCREEN_WIDTH - 26, 45)];
    bgView.backgroundColor = [UIColor whiteColor];
    [cell.contentView addSubview:bgView];
        
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, CGRectGetWidth(bgView.frame) - 20, 25)];
    label.font = [UIFont systemFontOfSize:15];
    label.textColor = [UIColor hex:@"333333"];
    label.tag = 10;
    [bgView addSubview:label];
    
    label.text = [self.dataArray safeObjectAtIndex:indexPath.row];
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 55.0f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        AboutViewController *about = [[AboutViewController alloc] init];
        about.title = @"关于思凯普";
        [self.navigationController pushViewController:about animated:YES];
        return;
    }
    if (indexPath.row == 1) {
        [[DataManager shareManager] cleanLocalCache];
        return;
    }
    else if (indexPath.row == 2){
        ChangePasswordViewController *changePassword = [[ChangePasswordViewController alloc] init];
        [self.navigationController pushViewController:changePassword animated:YES];
    }
    if (indexPath.row==3) {
        FanKuiSViewController *fan = [FanKuiSViewController new];
        [self.navigationController pushViewController:fan animated:YES];
    }else if(indexPath.row == 4){
    
        [SVProgressHUD showWithStatus:@"正在检查。。。" maskType:1];
        [self requestAPPVersion];
        
        //检查本地版本：
        NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
        CFShow((__bridge CFTypeRef)(infoDic));
        version = [infoDic objectForKey:@"CFBundleShortVersionString"];
    }else if (indexPath.row == 5){
        
        MyInforViewController *myInfoVC = [[MyInforViewController alloc]init];
        [self.navigationController pushViewController:myInfoVC animated:YES];
        
        
    }
}



- (void )requestAPPVersion
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    [manager POST:@"http://itunes.apple.com/lookup?id=1091154692" parameters:nil success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject) {
        
        NSArray *rootArray = responseObject[@"results"];
        
        NSDictionary *dict = [rootArray lastObject];
        
        NSLog(@"当前版本为：%@", dict[@"version"]);
        [SVProgressHUD dismiss];
        
        if ([version isEqualToString:dict[@"version"]]) {
            [self showAlertController:YES];
        }else{
            [self showAlertController:NO];
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable operation, NSError * _Nonnull error) {
        [SVProgressHUD showErrorWithStatus:@"请求失败,请稍后再试" ];
    }];
}

- (void)showAlertController:(BOOL)isTheSame
{
    UIAlertAction * actionCancel;
    UIAlertAction * actionSure;

    if(isTheSame == YES){
        _alertController = [UIAlertController alertControllerWithTitle:@"检查更新"
                                                               message:@"当前为最新版本."
                                                        preferredStyle:UIAlertControllerStyleAlert];
        actionSure = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                              handler:^(UIAlertAction * action) {
                                                                  
                                                                  NSLog(@"点击了确定");
                                                              }];
          [_alertController addAction:actionSure];

    }else{
        _alertController = [UIAlertController alertControllerWithTitle:@"检查更新"
                                                               message:@"有新版本更新^_^"
                                                        preferredStyle:UIAlertControllerStyleAlert];
        actionCancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault
                                               handler:^(UIAlertAction * action) {
                                                   
                                               }];
        actionSure = [UIAlertAction actionWithTitle:@"去App Store更新" style:UIAlertActionStyleDefault
                                               handler:^(UIAlertAction * action) {
                                        
                                                   //调用AppStore去下载更新:
                                                   [self openAppaleShop];
                                               }];
        
        [_alertController addAction:actionCancel];
        [_alertController addAction:actionSure];
    }
    
    [self presentViewController:_alertController animated:YES completion:nil];
}

- (void)openAppaleShop
{
    NSString *appleID = @"1091154692";
    NSString *strUrl = [NSString stringWithFormat:@"http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=%@&mt=8",appleID];
    NSURL *url = [NSURL URLWithString:strUrl];
    [[UIApplication sharedApplication] openURL:url];
    
    
}

@end
